package domain

import (
	"context"
	"jobcenter/internal/dao"
	"jobcenter/internal/database"
	"jobcenter/internal/model"
	"jobcenter/internal/repo"
	"log"
)

type KlineDomain struct {
	klineRepo repo.KlineRepo //repo.KlineRepo是一个接口，构造函数可以传入一个实例化的struct
}

func (k *KlineDomain) SaveBatch(data [][]string, symbol string, period string) {
	klines := make([]*model.Kline, len(data))
	for i, v := range data {
		klines[i] = model.NewKline(v, period)
	}
	//只要大于最后一个时间的数据，防止数据重复
	err := k.klineRepo.DeleteGtTime(context.Background(), klines[len(data)-1].Time, symbol, period)
	if err != nil {
		log.Println(err)
		return
	}
	//log.Printf("存储到mongo的数据:%v\n", klines)
	err = k.klineRepo.SaveBatch(context.Background(), klines, symbol, period)
	if err != nil {
		log.Println(err)
	}
}

func NewKlineDomain(client *database.MongoClient) *KlineDomain {
	klineDao := dao.NewKlineDao(client.Db)
	return &KlineDomain{
		klineRepo: klineDao,
	}
}
